package com.zy.experience_system.controller;

import com.zy.experience_system.entity.User;
import com.zy.experience_system.enums.PermsEnum;
import com.zy.experience_system.enums.ResultEnum;
import com.zy.experience_system.exception.CommonException;
import com.zy.experience_system.form.AccountForm;
import com.zy.experience_system.form.CreateAccountForm;
import com.zy.experience_system.service.AccountService;
import com.zy.experience_system.util.*;
import com.zy.experience_system.vo.AccountNameVO;
import com.zy.experience_system.vo.AccountVO;
import com.zy.experience_system.vo.ResultVO;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/user")
public class AccountController extends BaseController {


    @Autowired
    private AccountService accountService;

    @Autowired
    private JwtUtil jwtUtil;

    @GetMapping("/verify")
    public ResultVO verify(){
        return ResultUtil.success(null);
    }


    @PostMapping("/login")
    public ResultVO login(@Validated @RequestBody AccountForm accountForm, HttpServletResponse response) {
        AccountVO accountVO;
        try {
            accountVO = accountService.login(accountForm);
            return ResultUtil.success(accountVO);
        } catch (Exception e) {
            throw new CommonException(400,e.getMessage());
        }
    }

    @PostMapping("/profile")
    public ResultVO profile(HttpServletRequest request) throws Exception {

        /**
         * 从请求头信息中获取token数据
         *      1.获取请求头信息，名称=Authorization
         *      2.替换Bearer+空格
         *      3.解析token
         *      4.获取clamis
         */
        String username = claims.getId();

        AccountVO accountVO = accountService.findByUsername(username);
        accountVO.setToken(token);
        ResultVO resultVO = ResultUtil.success(accountVO);
        return resultVO;
    }

    @PostMapping("/create")
    public ResultVO create(
            @Validated @RequestBody CreateAccountForm createAccountForm) throws Exception{

        if (claims.get("perms").equals(PermsEnum.ADMIN.getPerms())) {
            try {
                accountService.save(createAccountForm);
                return ResultUtil.success(null);
            } catch (Exception e) {
//                System.out.println(e.toString());
                throw new CommonException(ResultEnum.FAIL);
            }
        }throw new CommonException(ResultEnum.NO_PERMS);

    }

    @PutMapping("/updatePwd")
    public ResultVO updatePwd(@Validated @RequestBody AccountForm accountForm) {
        String username = accountForm.getUsername();
        String password = accountForm.getPassword();
        String newpass = accountForm.getNewpass();
        try {
            if (accountService.findUserByUsername(username) == null) {
                throw new CommonException(400, "找不到该用户");
            } else {
                User user = accountService.findUserByUsername(username);
                if (MD5Util.getMD5(password).equals(user.getPassword())) {
                    accountService.update(username, MD5Util.getMD5(newpass));
                    return ResultUtil.success(null);
                }
                return ResultUtil.error("密码错误");
            }
        } catch (Exception e) {
            return ResultUtil.error("修改失败");
        }
    }

    @PutMapping("/setPwd")
    public ResultVO setPwd(@Validated @RequestBody AccountForm accountForm) {
        if (claims.get("perms").equals(PermsEnum.ADMIN.getPerms())) {
            String username = accountForm.getUsername();
            String password = accountForm.getPassword();
            try {
                if (accountService.findUserByUsername(username) == null) {
                    return ResultUtil.error("找不到该用户");
                } else {
                    accountService.update(username, MD5Util.getMD5(password));
                    return ResultUtil.success(null);

                }
            } catch (Exception e) {
                return ResultUtil.error("修改失败");
            }
        }throw new CommonException(ResultEnum.NO_PERMS);

    }

    @PostMapping("/createStu")
    public ResultVO allTest(@Validated @RequestBody String students) {
        if (claims.get("perms").equals(PermsEnum.ADMIN.getPerms())) {
            try {
                accountService.saveStu(students);
                return ResultUtil.success(null);
            }catch (Exception e){
                return ResultUtil.error("创建失败");
            }

        }throw new CommonException(ResultEnum.NO_PERMS);
    }

    @GetMapping("/allUser")
    public ResultVO allUser(){
        if (claims.get("perms").equals(PermsEnum.ADMIN.getPerms())) {
            try {
                List<AccountNameVO> accountNameVOList = accountService.findAll();
                return ResultUtil.success(accountNameVOList);
            }catch (Exception e){
                System.out.println(e.toString());
                return ResultUtil.error("查询失败");
            }

        }throw new CommonException(ResultEnum.NO_PERMS);
    }
}
